<?php
session_start();
include_once "checkLogin.php";
?>
<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="css/menu.css" rel="stylesheet" type="text/css"/>
    <script src="js/jquery-1.9.1.min.js" type="text/javascript"></script>
    <script src="js/nav.js" type="text/javascript"></script>
    <link rel="stylesheet" href="layui/css/layui.css">
    <script src="js/TweenMax.min.js"></script>
    <link href="css/word.css" rel="stylesheet" type="text/css"/>
    <title>管理员设置</title>
    <style>
        #content thead tr {
            background-color: #E2F5F7;
        }

        #content thead th {
            color: black;
            font-weight: bold;
            border-right: 2px solid white;
            text-align: center;
        }

        #content span a {
            color: skyblue;
            position: relative;
            top: 5px;
            left: 88%;
            font-size: 1.1em;
        }

        table td {
            color: black;
        }

        tbody td a {
            font-size: 1.1em;
            color: skyblue;
            cursor: pointer;
            position: relative;
            left: 17%;
        }

        #content .layui-unselect {
            margin-left: 30%;
        }
        #quanxian .layui-unselect{
            margin-left: 3%;
        }
    </style>
</head>
<body>
<canvas id="Mycanvas" style="position: absolute;left: 0%;width: 100%;height: 99%"></canvas>
<div id="main">
    <header>
        <div id="header1">
            <div id="a"></div>
            <img src="img/logo1.png">
            <h2 class="letter-wrap"  data-stagger="-0.015">清华大学图书馆管理系统</h2>
            <span>当前登录用户：<?php echo $_SESSION['admin']?></span>
        </div>
    </header>
    <div class="navmain">
        <div id="time"><span></span></div>
        <ul id="nav_all">
            <li><a href="index.php">首页</a></li>
            <?php
            include_once "conn.php";
            $main = new main();
            $info = $main->checkAdmin($_SESSION['admin']);
            if ($info['system']){
                ?>
                <li><a href="#">系统设置</a>
                    <ul style="display: none;">
                        <li><a href="library.php">图书馆信息</a></li>
                        <li><a href="admin.php">管理员设置</a></li>
                        <li><a href="#">参数设置</a></li>
                        <li><a href="shujia.php">书架设置</a></li>
                    </ul>
                </li>
                <?php
            }
            if ($info['readerset']){
                ?>
                <li><a href="#">读者管理</a>
                    <ul style="display: none;">
                        <li><a href="readertype.php">读者类型管理</a></li>
                        <li><a href="user.php">读者档案管理</a></li>
                    </ul>
                </li>
                <?php
            }
            if ($info['bookset']){
                ?>
                <li><a href="tushudangan.php">图书档案管理</a></li>
                <?php
            }
            if ($info['book']){
                ?>
                <li><a>图书借还</a>
                    <ul style="display: none;">
                        <li><a href="bookjieyue.php">图书借阅</a></li>
                        <li><a href="bookxujie.php">图书续借</a></li>
                        <li><a href="bookback.php">图书归还</a></li>
                    </ul>
                </li>
                <?php
            }
            if ($info['syschaxun']){
                ?>
                <li> <a>系统查询</a>
                    <ul style="display: none;">
                        <li><a href="tushudanganchaxun.php">图书档案查询</a></li>
                        <li><a href="jieyuechaxun.php">图书借阅查询</a></li>
                        <li><a href="jieyue.php">借阅到期提醒</a></li>
                    </ul>
                </li>
                <?php
            }
            ?>
            <li>
                <a href="pw.php">更改口令</a>
            </li>
            <li><a href="zhuxiao.php">注销</a> </li>
        </ul>
    </div>
    <div id="b"></div>
    <div id="c">
        <div id="now">当前位置：系统设置 > 管理员设置 >>></div>
        <div id="content" class="layui-form">
            <span><a href="#">添加管理员信息</a></span>
            <table class="layui-table" lay-even lay-skin="nob">
                <thead>
                <tr>
                    <th>管理员名称</th>
                    <th>系统设置</th>
                    <th>读者管理</th>
                    <th>图书档案管理</th>
                    <th>图书借还</th>
                    <th>系统查询</th>
                    <th>权限设置</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <?php
                include_once "conn.php";
                $main = new main();
                $result = $main->admin();
                for ($i = 0; $i < count($result); $i++) {
                    ?>
                    <tr>
                        <td id="name"><?php echo $result[$i]['name'] ?></td>
                        <td>
                            <input name="system" type="checkbox" value="<?php echo $result[$i]['system'] ?>"  <?php echo $result[$i]['system']>0 ? 'checked' : '' ?> disabled lay-skin="switch">
                        </td>
                        <td>
                            <input name="readerset" type="checkbox" value="<?php echo $result[$i]['readerset'] ?>"  lay-skin="switch" <?php echo $result[$i]['readerset']>0 ? 'checked' : '' ?> disabled>
                        </td>
                        <td>
                            <input name="bookset" class="a" value="<?php echo $result[$i]['bookset'] ?>"  type="checkbox"  lay-skin="switch" <?php echo $result[$i]['bookset']>0 ? 'checked' : '' ?> disabled>
                        </td>
                        <td>
                            <input name="book"  type="checkbox" value="<?php echo $result[$i]['book'] ?>"  lay-skin="switch" <?php echo $result[$i]['book']>0 ? 'checked' : '' ?> disabled>
                        </td>
                        <td>
                            <input name="syschaxun" type="checkbox" value="<?php echo $result[$i]['syschaxun'] ?>"  lay-skin="switch" <?php echo $result[$i]['syschaxun']>0 ? 'checked' : '' ?> disabled>
                        </td>
                        <td>
                            <a class="admin" href="#" id="<?php echo $result[$i]['id'] ?>"><button class="layui-btn">权限设置</button></a>
                        </td>
                        <td><a rel="<?php echo $result[$i]['id'] ?>"  user="<?php echo $result[$i]['name'] ?>" name=" <?php echo $_SESSION['admin'] ?> "><button class="layui-btn layui-btn-danger">删除</button></a></td>
                    </tr>
                    <?php
                }
                ?>
                </tbody>
            </table>
        </div>
    </div>
    <footer>
        <hr id="one">
        <hr id="two">
        CopyRight © 2019 www.cdp.cn.com 清华大学图书馆
    </footer>
</div>
</body>
<script src="layui/layui.js"></script>
<script src="js/logo.js"></script>
<script src="js/bg.js"></script>
<script>
    $(function () {
        $("tbody td a").click(function () {
            let id = $(this).attr("rel"); //对应id
            let names = $.trim($(this).attr("user")); //对应id
            let admin = $.trim($(this).attr("name")); //对应id
            if (this.innerText === "删除") {
                if (window.confirm("是否删除数据？")) {
                    if (names != admin ) {
                        $.ajax({
                            type: "POST",
                            url: "deladmin.php",
                            data: {
                                id: id
                            },
                            dataType: 'JSON',
                            cache: false, //不缓存此页面
                            success: function (data) {
                                console.log(data);
                                if (data.error === 1) {
                                    window.location.reload();
                                } else {
                                    alert('删除失败!')
                                }
                            }
                        })
                    }else{
                        layer.alert('禽兽连自己都不放过!', {
                            skin: 'layui-layer-molv' //样式类名
                            ,closeBtn: 0
                        }, function(){
                            layer.closeAll();
                        });
                    }
                }
            }
        })
    });
    layui.use(['form', 'layer'], function () {
        let layer = layui.layer,
            form = layui.form;
        $(".admin").click(function () {
            let id = $(this).attr("id");
            //console.log(id);
            $.ajax({
                type: "POST",
                url: "getadmin.php",
                data: {
                    id: id//对应id
                },
                dataType: 'JSON',

                cache: false, //不缓存此页面
                success: function (data) {
                    let name = data.name;
                    let a = data.system;
                    let b = data.readerset;
                    let c = data.bookset;
                    let d = data.book;
                    let e = data.syschaxun;
                    //console.log(name);
                     index = layer.open({
                        type: 1,
                        title: "权限设置",
                        closeBtn: 0,
                        skin: 'layui-layer-rim', //加上边框
                        area: ['520px', '310px'], //宽高
                        content: `<div style="margin: 15px 12px" class="layui-form" id="quanxian">
                            <label>管理员名称：</label>
                            <div class="layui-input-inline">
                            <input style="height: 25px;cursor: auto" type="text" name="name" value="${name}" required  autocomplete="off" class="layui-input" readonly>
                            </div><br><br>
                            <div>拥有的权限：</div>
                            <span style=";position:relative;top: 5px">系统权限:</span><input ${a>0?'checked':''}  type="checkbox" lay-skin="switch">
                            <span style=";position:relative;top: 5px;left:10px ;">读者管理：</span><input ${b>0?'checked':''}  type="checkbox" lay-skin="switch">
                            <span style=";position:relative;top: 5px;left:10px ;">图书档案管理：</span><input ${c>0?'checked':''} type="checkbox" lay-skin="switch"><br>
                            <span style=";position:relative;top: 5px;">图书借还:</span><input ${d>0?'checked':''} type="checkbox" lay-skin="switch">
                            <span style=";position:relative;top: 5px;left:10px ;">系统查询：</span><input ${e>0?'checked':''} type="checkbox" lay-skin="switch"><br>
                            <div style="margin-top: 50px;margin-left: 30%">
                                <button id="btn" class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
                                <button  type="reset" class="layui-btn layui-btn-primary" onclick="layer.closeAll()">关闭</button>
                            </div>
                            </div>`
                    });
                    document.getElementById("btn").addEventListener('click',function () {
                        var a = document.querySelectorAll('#quanxian input');
                        let input1 = a[1].checked === true ? 1 : 0;
                        let input2 = a[2].checked === true ? 1 : 0;
                        let input3 = a[3].checked === true ? 1 : 0;
                        let input4 = a[4].checked === true ? 1 : 0;
                        let input5 = a[5].checked === true ? 1 : 0;
                        //console.log(name,input1,input2,input3,input4,input5);
                        console.log(id,input1,input2,input3,input4,input5);
                        $.ajax({
                            type: "POST",
                            url: "addAdmin.php",
                            data: {
                                id:id,
                                input1:input1,
                                input2:input2,
                                input3:input3,
                                input4:input4,
                                input5:input5
                            },
                            dataType:'JSON',
                            cache: false, //不缓存此页面
                            success: function (data) {
                                if (data.error === 1){
                                    layer.msg('保存成功!');
                                    setTimeout(function(){
                                        window.location.reload();
                                    },600);
                                }
                                else {
                                    layer.msg('保存失败!');
                                }
                            },error(){
                                layer.msg('系统错误!');
                            }
                        })
                    });
                    var form = layui.form;
                    form.render();
                },error(){
                    alert('查看失败!');
                }
            });

        });
        $("#content span a").click(function () {

            layer.open({
                type: 1,
                title:'添加读者类型信息',
                closeBtn: 0, //不显示关闭按钮
                skin: 'layui-layer-rim', //加上边框
                area: ['450px', '320px'], //宽高
                content: `<div class="layui-form">
                                <div style="margin: 30px auto;margin-left: 30px">
                                    <div class="layui-form-item">
                                        <label style="width: 20%" class="layui-form-label">管理员名称：</label>
                                        <div class="layui-input-inline">
                                        <input type="text" name="name" id="tip" required lay-verify="required" placeholder="管理员名称" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label style="width: 20%"  class="layui-form-label">管理员密码：</label>
                                        <div class="layui-input-inline">
                                        <input type="password" id="pw1" name="repass" required lay-verify="required" placeholder="管理员密码" autocomplete="off" class="layui-input">
                                        </div>
                                    </div>
                                    <div class="layui-form-item">
                                        <label style="width: 20%"  class="layui-form-label">确认密码：</label>
                                        <div class="layui-input-inline">
                                        <input type="password" id="pw2" name="pw" required lay-verify="repass" placeholder="确认密码" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div style="margin-top: 25px;margin-left: 30%">
                                    <button id="btn" class="layui-btn" lay-submit lay-filter="formDemo">保存</button>
                                    <button  type="reset" class="layui-btn layui-btn-primary" onclick="layer.closeAll()">关闭</button>
                                </div>
                              </div>`
            });
            form.verify({
                repass: function() {
                    let pass = $("#pw1").val();
                    let pass1 = $("#pw2").val();
                    if(pass !== pass1) {
                        return '两次输入的密码不一致';
                    }
                }
            });
            form.on('submit(formDemo)', function(data){
                console.log(data.field);//当前容器的全部表单字段，名值对形式：{name: value}
                $.ajax({
                    type: "POST",
                    url: "add1admin.php",
                    data: {
                        name: data.field.name,
                        pw:data.field.pw
                    },
                    dataType: 'JSON',
                    cache: false, //不缓存此页面
                    success: function (data) {
                        if (data.error === 1) {
                            layer.msg('添加成功!');
                            setTimeout(function () {
                                window.location.reload();
                            },500)
                        }else if (data.error === 2) {
                            layer.tips('该管理员名称已经存在!请重试', '#tip', {
                                tips: [1, '#FF0000'],
                                time: 1000
                            });
                        }
                        else {
                            layer.msg('添加失败!请重试!');
                        }
                    }
                });
                return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
            });
        });

    });
</script>
</html>


